
// 节点抖动动画
class CShake extends cc.ActionInterval {
    private _initialPos:cc.Vec2 = cc.v2(0,0);
    private _strengthPos:cc.Vec2 = cc.v2(0,0);
 
    public initWithDuration(duration:number,x:number,y:number):boolean {
        // @ts-ignore
        cc.ActionInterval.initWithDuration(duration,x,y);
        this._strengthPos = cc.v2(x,y);
        return true;
    }
 
    private _fgRangeRand(min:number,max:number):number {
        let _rnd:number = Math.random();
        return _rnd * (max - min) + min;
    }
 
    public update(time:number):void {
        let _randx = this._fgRangeRand(-this._strengthPos.x,this._strengthPos.y);
        let _randy = this._fgRangeRand(-this._strengthPos.y,this._strengthPos.y);
        this.getTarget().setPosition(_randx + this._initialPos.x,_randy + this._initialPos.y);
    }
 
    public startWithTarget(target:cc.Node):void {
        // @ts-ignore
        cc.ActionInterval.startWithTarget(target);
        this._initialPos = cc.v2(target.x,target.y);
    }
 
    public stop():void {
        this.getTarget().setPosition(this._initialPos);
        // @ts-ignore
        cc.ActionInterval.stop();
    }
}

/**
 *  抖动动画
 * @param {number} duration     动画持续时长
 * @param {number} strength_x   抖动幅度： x方向
 * @param {number} strength_y   抖动幅度： y方向
 * @returns {Shake}
 * this.node.runAction(shake(1,15,15));
 */
export default  function shake(duration:number,x:number,y:number):CShake{
    let _act:CShake = new CShake();
    _act.initWithDuration(duration,x,y);
    return _act;
}
